<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList"
        :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="80"/>
            <el-table-column prop="content" label="内容" />
            <el-table-column label="发布人" align="center" prop="user.nickname"/>
            <el-table-column label="状态" width="200">
              <template slot-scope="scope">
                <el-select
                  :value="scope.row.status"
                  size="mini"
                  @change="(value) => handleStatusChange(scope.row, value)"
                  placeholder="请选择状态">
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="发布时间" width="180" />
            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "post",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.post,
      statusOptions: [
        { label: '正常', value: 1 },
        { label: '待审核', value: -1 },
        { label: '屏蔽', value: 0 }
      ],
      searchList: [
        {
          label: '标题',
          prop: 'title',
          name: 'input'
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          options: [
            { label: '正常', value: 1 },
            { label: '待审核', value: -1 },
            { label: '屏蔽', value: 0 }
          ]
        }
      ],
      searchParams: {
        title: '',
        status: ''
      },
      defaultParams: {
        title: '',
        status: ''
      },
      tableData: []
    }
  },
  methods: {
    handleStatusChange(row, value) {
      this.$confirm('确认修改该帖子的状态吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        http.post.update(row.id, { status: value }).then(() => {
          this.$message.success('修改成功');
          row.status = value;
        })
      })
    },
    getStatusType(status) {
      const statusMap = {
        1: 'success',
        '-1': 'warning',
        0: 'danger'
      }
      return statusMap[status] || 'info'
    },
    getStatusText(status) {
      const statusMap = {
        1: '正常',
        '-1': '待审核',
        0: '屏蔽'
      }
      return statusMap[status] || '未知'
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
